﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算某天星期几</title>
    <script type="text/javascript">
        function checkNum(str,min,max){ // 检测输入的数值
            if(str === ""){
                alert("请输入有效的值");
                return false;
            }
            for(let i = 0;i< str.length;i++){
                let ch = str.substring(i,i + 1);
                if(ch < "0" || ch > "9"){
                    alert("请输入数值");
                    return false;
                }
            }
            let val = parseInt(str,10);
            if((val < min ) || (val > max)){
                alert("请输入数值从 1 到" + max +".");
                return false;
            }
            return true;
        }
        function pushButton(form){
            //检查年月日的有效性                           
            if ((checkNum(form.day.value,1,31)) && 
                (checkNum(form.month.value,1,12)) && 
                (checkNum(form.year.value,0,2500))){
                var cur_day = parseInt(form.day.value,10);      //获取日
                var cur_month = parseInt(form.month.value,10);  //获取月
                var cur_year = parseInt(form.year.value,10);    //获取年
            }
            getDayOfWeek(cur_year+"-"+cur_month+"-"+cur_day);               //调用判断星期几的方法
        }
        
        function getDayOfWeek(dayValue){
            let day = new Date(Date.parse(dayValue.replace(/-/g, '/')));                      //将日期值格式化 
            let today = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");  //创建星期数组
            alert(today[day.getDay()])
        }
        
    </script>
</head>
<body>
<form NAME = "calDay">
<PRE><B>日:</B>   
<input TYPE="num" name="day" onChange="if (!checkNum(this.value, 1, 31)){
    this.focus();this.select();
} else {}" size=10 value="">        
<B>月:</B> 
<input TYPE="num" name="month" onChange="if (!checkNum(this.value, 1, 12)){
    this.focus();this.select();
}else {}" size=10 value="">
<B>年:</B>  
<input TYPE="num" name="year" onChange="if (!checkNum(this.value, 0, 2500)){
    this.focus();this.select();
} else {}" size=10 value="">        
<input TYPE="button" name="Find_Out" value="计算星期几" onclick="pushButton(this.form)"></PRE>
</form>
</body>
</html>